package com.jic.interaction.JDVOP.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jic.interaction.JDVOP.common.utils.JdVopHttpUtil;
import com.jic.interaction.JDVOP.common.utils.RestResult;
import com.jic.interaction.JDVOP.model.entity.*;
import com.jic.interaction.JDVOP.model.vo.response.*;
import com.jic.interaction.JDVOP.service.SiteApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @Classname SiteApiServiceImpl
 * @Description TODO
 * @Date 2020/11/16 19:23
 * @Created by 22720
 */
@Slf4j
@Service
public class SiteApiServiceImpl implements SiteApiService {

    @Value("${route.JDVOP}")
    private String url;

    /*
        查询京东一级地址列表。
     */
    @Override
    public RestResult selectOne(SiteApiOne one)throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("查询参数{}", one.toString());
        String u = url + "api/area/getProvince";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(one));
        log.info("返回结果{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString()!="true"){
            return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());

        }
        return RestResult.success(m.get("result").toString(), m.get("resultCode").toString(), m.get("resultMessage").toString());
         }

    /*
        根据京东一级地址ID，查询京东二级地址列表。
     */
    @Override
    public RestResult selectTwo(SiteApiTwo two)throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("查询参数{}", two.toString());
        String u = url + "api/area/getCity";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(two));
        log.info("返回结果{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString()!="true"){
            return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());

        }
        return RestResult.success(m.get("result").toString(), m.get("resultCode").toString(), m.get("resultMessage").toString());

    }

    /*
        根据京东二级地址ID，查询京东三级地址列表。
     */
    @Override
    public RestResult selectThree(SiteApiThree three)throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("查询参数{}", three.toString());
        String u = url + "api/area/getCounty";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(three));
        log.info("返回结果{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString()!="true"){
            return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());

        }
        return RestResult.success(m.get("result").toString(), m.get("resultCode").toString(), m.get("resultMessage").toString());
    }

    /*
        根据京东三级地址ID，查询京东四级地址列表。
     */
    @Override
    public RestResult selectFour(SiteApiFour four)throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("查询参数{}", four.toString());
        String u = url + "api/area/getTown";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(four));
        log.info("返回结果{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString()!="true"){
            return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());

        }
        return RestResult.success(m.get("result").toString(), m.get("resultCode").toString(), m.get("resultMessage").toString());
    }

    /*
        验证已编码为京东一至四级地址ID的有效性。
     */

    @Override
    public RestResult selectId(VerifiCationId cationId)throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("查询参数{}", cationId.toString());
        String u = url + "api/area/checkArea";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(cationId));
        log.info("返回结果{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString()!="true"){
            return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());

        }
        return RestResult.success(m.get("result").toString(), m.get("resultCode").toString(), m.get("resultMessage").toString());

    }


    /*
        根据地址详情转换为京东地址编码。
        该接口不能保证所有地址都匹配到京东地址，
        也不能保证所有匹配到的京东地址都正确。
        因而，优先推荐使用逐级选择的方法。
     */

    @Override
    public RestResult selectParticulars(ParticularsTransition particul)throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("查询参数{}", particul.toString());
        String u = url + "api/area/getJDAddressFromAddress";
        String s = JdVopHttpUtil.doRequestPost(u, BeanUtils.describe(particul));
        log.info("返回结果{}", s);
        Map m = JSONObject.parseObject(s);
        if (m.get("success").toString()!="true"){
            return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());

        }
        return RestResult.success(m.get("result").toString(), m.get("resultCode").toString(), m.get("resultMessage").toString());

    }
}
